package com.ximu.test.hibernate.web;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ximu.test.hibernate.HibernateSessionFactory;
import com.ximu.test.hibernate.bean.Message;
import com.ximu.test.hibernate.bean.User;



/**
 * struts2 + hibernate 分页
 * @author user
 */
public class MessageList{
	
	public String list(){
		this.setData(0,1);
		return "list";
	}
	
	private void setData(Integer start,Integer size){
		Session session = HibernateSessionFactory.getSessionFactory().openSession();
		String count = session.createQuery("select count(m.id) from Message as m").list().get(0).toString();
		List<Message> messageList = session.createQuery("from Message").setFirstResult(start).setMaxResults(size).list();
		session.close();
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("count", count);
		request.setAttribute("messageList",messageList);
	}
	
	public String del(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Integer id = Integer.parseInt(request.getParameter("id"));
		Message message = new Message();
		message.setId(id);
		try{
			Session session = HibernateSessionFactory.getSessionFactory().openSession();
			Transaction transaction = session.beginTransaction();
			session.delete(message);
			transaction.commit();
			session.close();
		}catch(HibernateException e){
			System.out.println("删除失败");
//			e.printStackTrace();
		}
		this.setData(0,1);
		return "list";
	}
	
	public String add(){
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String contents = request.getParameter("contents");
		if(contents==null){
			return "add";
		}
		String name = request.getParameter("user");
		Message message = new Message();
		message.setContents(contents);
		message.setIp(0);
		message.setPid(0);
		message.setTime(0);
		
		Set<Message> messageSet = new HashSet<Message>();
		messageSet.add(message);
		
		User user = new User();
		user.setName(name);
		user.setMessage(messageSet);
		
		Session session = HibernateSessionFactory.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.persist(user);
		transaction.commit();
		session.close();
		return "add";
	}
	
	public String edit(){
		HttpServletRequest request = ServletActionContext.getRequest();		String id = request.getParameter("id");
		String name = request.getParameter("name");
		Session session = HibernateSessionFactory.getSessionFactory().openSession();
		if(name!=null){
			String contents = request.getParameter("contents");
			List<Message> list = session.createQuery("from Message m where m.id = :id").setString("id",id).list();
			Message message = list.get(0);
			message.setContents(contents);
			User user = message.getUser();
			user.setName(name);
			message.setUser(user);
			Transaction  transaction  = session.beginTransaction();
			session.saveOrUpdate(message);
			transaction.commit();
		}
		List<Message> list = session.createQuery("from Message m where m.id = :id").setString("id",id).list();
		if(!list.isEmpty()){
			request.setAttribute("message", list.get(0));
		}
		session.close();
		return "edit";
	}
}
